﻿#include "myhashtable.h"

void HandleTable::Resize() {
	uint32_t new_length = 4;
	while (new_length < elems_) {
		new_length *= 2;
	}
	LRUHandle** new_list = new LRUHandle*[new_length];
	memset(new_list, 0, sizeof(new_list[0]) * new_length);
	uint32_t count = 0;
	for (uint32_t i = 0; i < length_; i++) {
		LRUHandle* h = list_[i];
		while (h != NULL) {
			LRUHandle* next = h->next_hash;
			Octets key = h->key();
			uint32_t hash = h->hash;
			LRUHandle** ptr = &new_list[hash & (new_length - 1)];
			h->next_hash = *ptr;
			*ptr = h;
			h = next;
			count++;
		}
	}
	assert(elems_ == count);
	delete[] list_;
	list_ = new_list;
	length_ = new_length;
}